﻿using System;
using System.Data.SqlClient;
using System.Configuration;

namespace WebShopDAL
{
    public class ConnectionDB
    {
        public static string ConnectionString
        {
            get
            {
                try
                {
                    string connectionString = ConfigurationManager.ConnectionStrings["Klauserswag"].ToString();

                    var newConnectionString = new SqlConnectionStringBuilder(connectionString);
                    newConnectionString.ApplicationName = ApplicationName ?? newConnectionString.ApplicationName;
                    newConnectionString.ConnectTimeout = (ConnnectionTimeOut > 0)
                        ? ConnnectionTimeOut
                        : newConnectionString.ConnectTimeout;

                    return newConnectionString.ToString();
                }
                catch (Exception ex)
                {
                    throw new KlauserSwagException("Something went wrong in ConnectionDB, ConnectionString", ex);
                }
            }
        }
        public static int ConnnectionTimeOut { get; set; }

        public static string ApplicationName { get; set; }

        public static SqlConnection GetConnection()
        {
            if (ConnectionString != null)
            {
                var connection = new SqlConnection(ConnectionString);
                if (connection == null)
                    return connection;

                connection.Open();
                return connection;
            }
            return null;
        }
    }
}
